package com.ruoyi.system.service.module.product.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.TransactionalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MathUtil;
import com.ruoyi.common.utils.OrderUtil;
import com.ruoyi.common.utils.wx.WxAppPay;
import com.ruoyi.common.utils.yilianprint.LAVApi;
import com.ruoyi.system.domain.module.dto.MerTableDto;
import com.ruoyi.system.domain.module.dto.OrderRestaurantSettleDto;
import com.ruoyi.system.domain.module.mer.*;
import com.ruoyi.system.domain.module.product.*;
import com.ruoyi.system.domain.module.excel.ProductOrderExcel;
import com.ruoyi.system.domain.module.excel.ProductOrderMerDetailExcel;
import com.ruoyi.system.domain.module.excel.ProductOrderMerTotalExcel;
import com.ruoyi.system.domain.vo.MonthMoneyVo;
import com.ruoyi.system.mapper.module.mer.*;
import com.ruoyi.system.mapper.module.product.ProductOrderMapper;
import com.ruoyi.system.mapper.module.product.ProductOrderTableMapper;
import com.ruoyi.system.service.module.product.*;
import com.ruoyi.system.service.module.wx.IWxTemplateService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Service
public class ProductOrderServiceImpl extends ServiceImpl<ProductOrderMapper, ProductOrder> implements IProductOrderService {

	@Resource
	private ProductOrderMapper orderMapper;
	@Resource
	private MerMapper merMapper;
	@Resource
	private IProductOrderInfoService productOrderInfoService;
	@Resource
	private ProductOrderTableMapper productOrderTableMapper;
	@Resource
	private IProductCartService cartService;

	@Resource
	private IProductService productService;

	@Value("${web.appid}")
	private String appid;
	@Value("${web.secret}")
	private String secret;

	@Override
	public Double selectTotalMoneyByTime(Map<String,Object> map) {
		return orderMapper.selectTotalMoneyByTime(map);
	}

	@Override
	public Double selectFenRunPlatform(Map<String, Object> map) {
		return orderMapper.selectFenRunPlatform(map);
	}

	@Override
	public Double selectFenRunProperty(Map<String, Object> map) {
		return orderMapper.selectFenRunProperty(map);
	}

	@Override
	public Double selectFenRunEstate(Map<String, Object> map) {
		return orderMapper.selectFenRunEstate(map);
	}

	@Override
	public Map<String, Object>  selectFenRunMer(Map<String, Object> map) {
		return orderMapper.selectFenRunMer(map);
	}

	@Override
	public List<MonthMoneyVo> selectTotalMoneyByDay(Map<String, Object> map) {
		return orderMapper.selectTotalMoneyByDay(map);
	}

	@Override
	public List<ProductOrderExcel> selectExcelListByTime(Map<String, Object> map) {
		return orderMapper.selectExcelListByTime(map);
	}

	@Override
	public List<ProductOrderMerTotalExcel> selectExcelMerTotalListByTime(Map<String, Object> map) {
		return orderMapper.selectExcelMerTotalListByTime(map);
	}

	@Override
	public List<ProductOrderMerDetailExcel> selectExcelMerDetailListByTime(Map<String, Object> map) {
		return orderMapper.selectExcelMerDetailListByTime(map);
	}

	@Override
	public List<ProductOrderExcel> selectPageExcelListByTime(Page<ProductOrderExcel> page, Map<String, Object> map) {
		return orderMapper.selectExcelListByTime(page,map);
	}

	@Override
	public ProductOrderExcel selectTongjiByEstateId(Map<String, Object> map) {
		return orderMapper.selectTongjiByEstateId(map);
	}

	@Override
	public int refundOp(ProductOrder order) {
		int ret = 0;
		try{
			String tradeNo = OrderUtil.getPayNo();
			order.setRefundOutTradeNo(tradeNo);
//			Map<String,Object> map = new HashMap<>();
//			WxAppPay wxAppPay = new WxAppPay(wx_refund_notify_url, secret, appid, mch_id,api_key);
//			Map<String, String> retRequest = wxAppPay.refundPayAPI(order.getOutTradeNo(),order.getPayPrice(),order.getPayPrice(),order.getRefundOutTradeNo(),keyPath);
//			if(retRequest.containsKey("return_code")){
//				String result_code = retRequest.get("result_code");
//				String err_code_des = retRequest.containsKey("err_code_des") ? retRequest.get("err_code_des") : "";
//				map.put("result_code",result_code);
//				map.put("err_code_des",err_code_des);
//				if(result_code.equals("SUCCESS")){
//					order.setRefundStatus(7);//支付成功
//				}
//			}
			ret = orderMapper.updateById(order);
		}catch (Exception e){
			e.printStackTrace();
		}
		return ret;
	}

}
